/**
 * Created by lobin on 2017/7/24.
 */
import { Application } from 'egg';
import * as moment from 'moment';
import * as mongoose from 'mongoose';
import * as pageFindPlugin from 'mongoose-paginate';
const ObjectId = mongoose.Schema.Types.ObjectId;
const packagesSchema = {
        intime: {
                type: Date,
                required: true,
                default: Date.now,
                get: (val: string) => moment(val).format('YYYY-MM-DD HH:mm:ss'),
        },
        outtime: {
                type: Date,
                required: false,
                get: (val: string) => val ? moment(val).format('YYYY-MM-DD HH:mm:ss') : '',
        },
        code: {
                type: String,
                required: true,
                trim: true,
        },
        name: String,
        mtype: {
                type: String,
                required: true,
                default: 9, // 0短信 1微信 9未发送 11发送失败
        },
        shelf: {
                id: {
                        type: ObjectId,
                },
                name: String,
                nano: String,
        },
        site: {
                type: ObjectId,
                required: true,
                ref: 'Site',
        },
        partner: {
                id: {
                        type: ObjectId,
                },
                name: String,
                pinyin: String,
        },
        unitprice: {
                type: Number,
                default: 30,
        },
        phone: {
                type: String,
                required: true,
        },
        secret: {
                type: String,
                required: true,
        },
        serial: {
                type: String,
                required: true,
        },
        status: {
                type: String,
                default: 0,
                required: true,
        },
        smsstatus: {
                type: String,
        },
        desc: {
                type: String,
                default: '无',
        },
        conectStatus: {
                in: {
                        type: Boolean,
                        default: false,
                        required: false,
                },
                problem: {
                        type: Boolean,
                        default: false,
                        required: false,
                },
                out: {
                        type: Boolean,
                        default: false,
                        required: false,
                },
        },
        logs: {
                type: Array,
                required: true,
        },
};
const queryIndex = {
        site: 1,
        phone: 1,
        secret: 1,
        intime: -1,
        status: 1,
        'partner.id': 1,
        code: 1,
        mtype: 1,
        outtime: 1,
};
const partnerIndex = {
        code: 1,
        'partner.id': 1,
};
const schemaOptions = {
        id: false,
        toJSON: {
            getters: true,
            virtuals: true,
        },
};
export default (app: Application) => {
        const mongoose = (app as any).mongoose;
        const conn = (app as any).mongooseDB.get('bmtx');
        const schema = new mongoose.Schema(packagesSchema, schemaOptions);
        schema.index(queryIndex);
        schema.index(partnerIndex, { unique: true });
        schema.plugin(pageFindPlugin, {});
        return conn.model('Packages', schema, 'packages');
};
export const schemaObj = packagesSchema;
export const partnerIndexObj = partnerIndex;
export const queryIndexObj = queryIndex;
export const options = schemaOptions;
